package main

import (
	"errors"
	"fmt"
	"sort"
	"strconv"
)

type LinkedList struct {
	Head *Node
	Tail *Node
	Size int
}

type Node struct {
	Val  interface{}
	Pre  *Node
	Next *Node
}

func (this *LinkedList) Add(val interface{}) (index int) {

	newNode := &Node{Val: val, Next: nil}

	this.Size ++

	if this.Head == nil {

		this.Head = newNode
		this.Tail = newNode
		return this.Size
	}

	//新节点的上一个节点是链表的最后
	newNode.Pre = this.Tail
	//把新节点放到链表的最后
	this.Tail.Next = newNode
	//把新节点放到链表的最后
	this.Tail = newNode

	return this.Size

}
func (this *LinkedList) addByIndex(index int, val interface{}) {



	newNode := &Node{Val: val, Next: nil}

	if index > this.Size {

		panic("老铁太大了")
	}


	defer func() {
		fmt.Println("this.size ++ " , this.Size,this.Size+1)
		this.Size++

	}()

	curNode := this.Head
	in := 1

	if  index == 0 {

		if this.Head == nil {
			this.Head = newNode
			this.Tail = newNode
		}else{
			this.Head = newNode
			newNode.Next = curNode
			curNode.Pre = newNode
		}
		return
	}

	if index == this.Size{

		this.Tail.Next = newNode
		newNode.Pre = this.Tail
		this.Tail = newNode
		return
	}



	for true {

		if in == index {

			curNode.Pre.Next = newNode
			//   2
			// 1 2 3
			//新节点的上个节点等于当前节点
			newNode.Next = curNode

			newNode.Pre = curNode.Pre

			curNode.Pre = newNode

/*
				//当前节点的下一个节点等于新节点
				curNode.Pre = newNode
				this.Tail = newNode


			//新节点的下一个节点等于当前节点的下一个节点
			newNode.Next = curNode.Next

			//当前节点的下一个节点的上一个节点等于 新节点
			curNode.Next.Pre = newNode

			//当前节点的下一个节点等于新节点
			curNode.Pre = newNode*/
			break
		}

		in++
		curNode = curNode.Next
	}



}

func (this *LinkedList) remove(index int) (val interface{}, err error) {

	if index >= this.Size {

		return nil, errors.New("越界了")
	}

	in := 0
	curNode := this.Head
	for true {

		if in == index {

			if curNode == this.Head {

				this.Head = curNode.Next

			} else {

				curNode.Pre.Next = curNode.Next
			}

			if this.Tail == curNode {

				this.Tail = curNode.Pre

			} else {

				curNode.Next.Pre = curNode.Pre
			}

			this.Size--
			return curNode.Val, nil

		}else{
			curNode = curNode.Next
		}

		in++

	}



	return nil, errors.New("未找到节点")
}

func (this *LinkedList) Show() {

	node := this.Head

	for i := 0; i < this.Size; i++ {

		fmt.Printf("linkedList[%d] = %v \n", i, node.Val, )

		node = node.Next
	}
}

func main() {

	linkedList := LinkedList{}

	iii := []int{3, 2, 1}

	sort.Ints(iii)

	fmt.Println(iii)

	var command string
	var val string
	for true {
		fmt.Println("请输入命令")
		fmt.Scan(&command)

		switch command {

		case "add":
			fmt.Println("请输入你要添加的值")
			fmt.Scan(&val)
			index := linkedList.Add(val)
			fmt.Println("index:", index, "val : ", val)
		case "add2":
			fmt.Println("请输入你要添加的index")

			var in int
			fmt.Scan(&in)

			fmt.Println("请输入你要添加的值")

			fmt.Scan(&val)

			linkedList.addByIndex(in, val)

			fmt.Println("index:", in, "val : ", val)
		case "take":
			fmt.Println("请输入你要删除的值")
			fmt.Scan(&val)
			i, _ := strconv.Atoi(val)
			data, _ := linkedList.remove(i)
			fmt.Println("data:", data)
		case "size":
			fmt.Println(linkedList.Size)
		case "show":

			node := linkedList.Head

			for i := 0; i < linkedList.Size; i++ {

				fmt.Printf("linkedList[%d] = %v \t,", i, node.Val, )

				node = node.Next
			}

			fmt.Println()

		}

	}
}
